package com.study.langchain4j.config.rag;

import com.study.langchain4j.config.Langchain4jConfig;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xl-9527
 * @since 2025/11/3
 */
@Configuration(proxyBeanMethods = false)
public class EmbeddingModelConfig {

    @Bean
    EmbeddingModel embeddingModel(@Value("${dashscope.api-key}") String apiKey) {
        return new OpenAiEmbeddingModel.OpenAiEmbeddingModelBuilder()
                .apiKey(apiKey)
                .baseUrl(Langchain4jConfig.BASE_URL)
                .logRequests(true)
                .logResponses(true)
                .modelName("text-embedding-v4")
                .build();
    }

    @Bean
    @ConditionalOnMissingBean(EmbeddingStore.class)
    EmbeddingStore<String> memoryEmbeddingStore() {
        return new InMemoryEmbeddingStore<>();
    }
}
